//
//  AM_HomeMainView.m
//  AppleMarket
//
//  Created by yangy on 18/10/2018.
//  Copyright © 2018 szgsy. All rights reserved.
//

#import "AM_HomeMainView.h"

// view
#import "LMJScrollTextView2.h"
#import "UnLoginAssetsView.h"
#import "MarketListView.h"
#import "AssetView.h"
// videMOdel
#import "HomeMainViewModel.h"
// model
#import "BannerModel.h"
#import "NewsModel.h"

@interface AM_HomeMainView()<SDCycleScrollViewDelegate,LMJScrollTextView2Delegate>
@property (nonatomic, strong) UIScrollView *scrollerView;
@property (nonatomic, strong) SDCycleScrollView *bannerScrollerView; //广告图
@property (nonatomic, strong) LMJScrollTextView2 *adTextView;       // 公告
@property (nonatomic, strong) UnLoginAssetsView *loginedView;         // 未登录
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) MarketListView *listView;             // 涨幅榜
@property (nonatomic, strong) HomeMainViewModel *viewModel;
@property (nonatomic, strong) AssetView *asssetView;               // 资产视图
@end

@implementation AM_HomeMainView

#define topImageHeight 172
- (void)dealloc
{
    [kNotificationCenter removeObserver:self name:kUserDidLoginNotification object:nil];
    [kNotificationCenter removeObserver:self name:kUserDidLoginOutNotification object:nil];
}
- (instancetype)initWithFrame:(CGRect)frame viewModel:(HomeMainViewModel*) viewModel
{
    self = [super initWithFrame:frame];
    if(self){
        
        _viewModel = viewModel;
        
        [self addSubview:self.scrollerView];
        [self.scrollerView addSubview:self.bannerScrollerView];
        [self.scrollerView addSubview:self.adTextView];
        [self.scrollerView addSubview:self.asssetView];
        [self.scrollerView addSubview:self.loginedView];
        [self.scrollerView addSubview:self.lineView];
        [self.scrollerView addSubview:self.listView];
        
        [self.scrollerView setContentSize:CGSizeMake(k_ScreenWidth, CGRectGetMaxY(_lineView.frame) + [self.listView heightForTableView])];
        // 加载banner图数据
        [self bindViewModel];
        
        [[[kNotificationCenter rac_addObserverForName:kUserDidLoginNotification object:nil] throttle:1] subscribeNext:^(NSNotification * _Nullable x) {
            // 登录成功
            [self refershViewLayout];
            [self.viewModel.coinVM.refershAssetCommand execute:nil];
        }];
        
        [[kNotificationCenter rac_addObserverForName:kUserDidLoginOutNotification object:nil] subscribeNext:^(NSNotification * _Nullable x) {
            [self refershViewLayout];
        }];
        
        [self refershViewLayout];
    }
    return self;
}

- (void)  bindViewModel
{

    [self.viewModel.refershBannerCommond execute:nil];
    [self.viewModel.refershNewsCommond execute:nil];
    @weakify(self);
    // 刷新banner数据
    [self.viewModel.refershEndSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        NSArray *imgArray = [[self.viewModel.bannerArray.rac_sequence map:^id _Nullable(BannerModel *value) {
            return value.Img;
        }] array];
        self.bannerScrollerView.imageURLStringsGroup = imgArray;
    }];
    
    // 刷新公告信息
    [self.viewModel.refershNewsEndSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        if(kArrayIsEmpty(self.viewModel.newsArray)){
            [self.adTextView setTextDataArr:@[@"暂无公告"]];
        }else{
            NSArray *titles = [[self.viewModel.newsArray.rac_sequence map:^id _Nullable(NewsModel * value) {
                return value.DocTitle;
            }] array];
            [self.adTextView setTextDataArr:titles];
        }
    }];
}

- (void) refershViewLayout
{
    if([PlistCommon loginModel] && [PlistCommon loginModel].UserID != 0){
        self.loginedView.hidden = YES;
        self.asssetView.hidden = NO;
        _lineView.y = CGRectGetMaxY(_asssetView.frame);
        _listView.y = CGRectGetMaxY(_lineView.frame);
        [self.scrollerView setContentSize:CGSizeMake(k_ScreenWidth, CGRectGetMaxY(_lineView.frame) + [self.listView heightForTableView])];
        [self setNeedsLayout];
    }else{
        self.loginedView.hidden = NO;
        self.asssetView.hidden = YES;
        _lineView.y = CGRectGetMaxY(self.loginedView.frame);
        _listView.y = CGRectGetMaxY(_lineView.frame);
        [self.scrollerView setContentSize:CGSizeMake(k_ScreenWidth, CGRectGetMaxY(_lineView.frame) + [self.listView heightForTableView])];
        [self setNeedsLayout];
    }
}

#pragma mark - SDCycleScrollViewDelegate
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    // 点击banner图
    if(self.viewModel.bannerArray.count > index){
        BannerModel *m = self.viewModel.bannerArray[index];
        [self.viewModel.bannerClickSubject sendNext:m];
    }
}

#pragma mark - LMJScrollTextView2Delegate
- (void)scrollTextView2:(LMJScrollTextView2 *)scrollTextView clickIndex:(NSInteger)index content:(NSString *)content
{
    // 点击公告消息
    if(self.viewModel.newsArray.count > index){
        NewsModel *m = self.viewModel.newsArray[index];
        [self.viewModel.newsClickSubject sendNext:m];
    }
}

- (void)scrollTextView2DidMoreClick:(LMJScrollTextView2 *)scrollTextView
{
    [self.viewModel.moreClickSubject sendNext:nil];
}

#pragma mark - lazy load
- (UIScrollView *)scrollerView
{
    if(!_scrollerView){
        _scrollerView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, k_ScreenWidth, k_ScreenHeight - NAVIGATION_BAR_HEIGHT - TAB_BAR_HEIGHT)];
    }
    return _scrollerView;
}

- (SDCycleScrollView *)bannerScrollerView
{
    if(!_bannerScrollerView){
        CGRect frame = CGRectMake(15, 0, k_ScreenWidth - 30, topImageHeight);
        _bannerScrollerView = [SDCycleScrollView cycleScrollViewWithFrame:frame delegate:self placeholderImage:nil];
        _bannerScrollerView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
        _bannerScrollerView.showPageControl = YES;
        _bannerScrollerView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
        //当前分页控件小圆标颜色
        _bannerScrollerView.currentPageDotColor = kColorWithHex(0x3291EB);
        //其他分页控件小圆标颜色
        _bannerScrollerView.pageDotColor = kColorWithHex(0x747474);
    }
    return _bannerScrollerView;
}

- (LMJScrollTextView2 *)adTextView
{
    if(!_adTextView){
        CGRect frame = CGRectMake(0, CGRectGetMaxY(_bannerScrollerView.frame), k_ScreenWidth, 43);
        _adTextView = [[LMJScrollTextView2 alloc] initWithFrame:frame];
        _adTextView.delegate = self;
        _adTextView.textFont = kFont(13);
        _adTextView.textColor = kColorWithHex(0x50565C);
        _adTextView.textAlignment = NSTextAlignmentLeft;
        [_adTextView startScrollBottomToTopWithNoSpace];
    }
    return _adTextView;
}

- (UnLoginAssetsView *)loginedView
{
    if(!_loginedView){
        _loginedView = [[UnLoginAssetsView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_adTextView.frame), k_ScreenWidth, 88) viewModel:self.viewModel.assetsVM];
    }
    return _loginedView;
}


- (UIView *)lineView
{
    if(!_lineView){
        _lineView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_loginedView.frame), k_ScreenWidth, 10)];
        _lineView.backgroundColor = kColorWithHex(0xF7F9FC);
    }
    return _lineView;
}

- (MarketListView *)listView
{
    if(!_listView){
        _listView = [[MarketListView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_lineView.frame), k_ScreenWidth, 200) viewModel:self.viewModel.marketVM];
        @weakify(self);
        _listView.heightChangeBlock = ^(){
            @strongify(self);
            [self.scrollerView setContentSize:CGSizeMake(k_ScreenWidth, CGRectGetMaxY(_lineView.frame) + [self.listView heightForTableView])];
        };
    }
    return _listView;
}

- (AssetView *)asssetView
{
    if(!_asssetView){
        _asssetView = [[AssetView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_adTextView.frame), k_ScreenWidth, 248) viewModel:self.viewModel.coinVM];
        _asssetView.hidden = YES;
    }
    return _asssetView;
}

@end
